home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************\
-
- File: ls main window.c
-
- Purpose: This module handles initializing/drawing/dealing with the
- main window.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program in a file named "GNU General Public License".
- If not, write to the Free Software Foundation, 675 Mass Ave,
- Cambridge, MA 02139, USA.
-
- \**********************************************************************/
-
- #include "ls main window.h"
- #include "ls meat.h"
- #include "ls endgame.h"
- #include "ls find.h"
- #include "environment.h"
- #include "graphics.h"
- #include "util.h"
- #include "menus.h"
- #include "sounds.h"
- #include "program globals.h"
-
- enum { key_LeftArrow=0x1c, key_RightArrow, key_UpArrow, key_DownArrow };
-
- /* internal procedures for ls main window.c only */
- static void SetupTheMainWindow(WindowDataHandle theData);
- static void ShutDownTheMainWindow(WindowDataHandle theData);
- static void InitializeTheMainWindow(WindowDataHandle theData);
- static void OpenTheMainWindow(WindowDataHandle theData);
- static void CloseTheMainWindow(WindowDataHandle theData);
- static void IdleInMainWindow(WindowDataHandle theData);
- static void KeyPressedInMainWindow(WindowDataHandle theData, unsigned char theChar);
- static void MouseClickedInMainWindow(WindowDataHandle theData, Point thePoint);
- static void DisposeTheMainWindow(WindowDataHandle theData);
- static void ActivateTheMainWindow(WindowDataHandle theData);
- static void DeactivateTheMainWindow(WindowDataHandle theData);
- static void DrawTheMainWindow(WindowDataHandle theData, short theDepth);
-
- static short gOldForegroundTime; /* stored foreground wait time */
- static short gDisplayedScore;
-
- short MainWindowDispatch(WindowDataHandle theData, short theMessage,
- unsigned long misc)
- {
- short theDepth;
- unsigned char theChar;
- Point thePoint;
- GrafPtr curPort;
-
- switch (theMessage)
- {
- case kNull:
- IdleInMainWindow(theData);
- return kSuccess;
- break;
- case kUpdate:
- theDepth=misc&0x7fff;
- DrawTheMainWindow(theData, theDepth);
- return kSuccess;
- break;
- case kInitialize:
- InitializeTheMainWindow(theData);
- return kFailure;
- case kOpen:
- OpenTheMainWindow(theData);
- return kSuccess;
- break;
- case kClose:
- CloseTheMainWindow(theData);
- return kSuccess;
- break;
- case kKeydown:
- theChar=misc&charCodeMask;
- KeyPressedInMainWindow(theData, theChar);
- return kSuccess;
- break;
- case kMousedown:
- thePoint.h=(misc>>16)&0x7fff;
- thePoint.v=misc&0x7fff;
- MouseClickedInMainWindow(theData, thePoint);
- return kSuccess;
- break;
- case kActivate:
- ActivateTheMainWindow(theData);
- return kSuccess;
- break;
- case kDeactivate:
- DeactivateTheMainWindow(theData);
- return kSuccess;
- break;
- case kStartup:
- SetupTheMainWindow(theData);
- return kSuccess;
- break;
- case kDispose:
- DisposeTheMainWindow(theData);
- return kSuccess;
- break;
- case kShutdown:
- ShutDownTheMainWindow(theData);
- return kSuccess;
- break;
- case kUndo:
- if (!ShowingEndGameQQ())
- {
- if (UndoOneMove(theData))
- DoSound(sound_undo, TRUE);
- else
- DebugStr("\pUndoOneMove failed");
- }
- return kSuccess;
- break;
- }
-
- return kFailure;
- }
-
- void SetupTheMainWindow(WindowDataHandle theData)
- {
- unsigned char *titleStr="\pLose Your Marbles!";
- short i;
-
- (**theData).maxDepth=8;
- (**theData).windowType=noGrowDocProc;
- (**theData).hasCloseBox=TRUE;
- Mymemcpy((Ptr)((**theData).windowTitle), (Ptr)titleStr, titleStr[0]+1);
-
- for (i=0; i<10; i++)
- {
- if (gHasColorQD)
- gTheCIcon[i]=GetCIcon(i+128);
- else
- gTheIcon[i]=GetIcon(i+128);
- }
- }
-
- void ShutDownTheMainWindow(WindowDataHandle theData)
- {
- short i;
-
- for (i=0; i<10; i++)
- {
- if (gHasColorQD)
- DisposeCIcon(gTheCIcon[i]);
- else
- ReleaseResource(gTheIcon[i]);
- }
- }
-
- void InitializeTheMainWindow(WindowDataHandle theData)
- {
- (**theData).windowWidth=gNumColumns*38+2;
- (**theData).windowHeight=gNumRows*38+2;
- DoSound(sound_startgame, TRUE);
- }
-
- void OpenTheMainWindow(WindowDataHandle theData)
- {
- (**theData).offscreenNeedsUpdate=TRUE;
- }
-
- void CloseTheMainWindow(WindowDataHandle theData)
- {
- }
-
- void IdleInMainWindow(WindowDataHandle theData)
- {
- }
-
- void KeyPressedInMainWindow(WindowDataHandle theData, unsigned char theChar)
- {
- short theValue;
- Rect tempRect;
- Point currentPoint;
-
- ObscureCursor();
-
- if (ShowingEndGameQQ())
- {
- DontShowEndGame(theData);
- return;
- }
-
- if (ShowingFindQQ())
- {
- DontShowFind(theData);
- return;
- }
-
- if (GameOverQQ())
- return;
-
- theValue=-1;
- switch (theChar)
- {
- case '0':
- theValue=10;
- break;
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- theValue=theChar-'0';
- break;
- case ' ':
- case 0x08:
- theValue=0;
- break;
- case 'j':
- case 'J':
- case key_LeftArrow:
- FramePosition(gCurrentRow, gCurrentColumn, FALSE);
- gCurrentColumn--;
- if (gCurrentColumn<0)
- gCurrentColumn=gNumColumns-1;
- FramePosition(gCurrentRow, gCurrentColumn, TRUE);
- (**theData).offscreenNeedsUpdate=TRUE;
- break;
- case 'i':
- case 'I':
- case key_UpArrow:
- FramePosition(gCurrentRow, gCurrentColumn, FALSE);
- gCurrentRow--;
- if (gCurrentRow<0)
- gCurrentRow=gNumRows-1;
- FramePosition(gCurrentRow, gCurrentColumn, TRUE);
- (**theData).offscreenNeedsUpdate=TRUE;
- break;
- case 'k':
- case 'K':
- case key_RightArrow:
- FramePosition(gCurrentRow, gCurrentColumn, FALSE);
- gCurrentColumn++;
- if (gCurrentColumn>=gNumColumns)
- gCurrentColumn=0;
- FramePosition(gCurrentRow, gCurrentColumn, TRUE);
- (**theData).offscreenNeedsUpdate=TRUE;
- break;
- case 'm':
- case 'M':
- case key_DownArrow:
- FramePosition(gCurrentRow, gCurrentColumn, FALSE);
- gCurrentRow++;
- if (gCurrentRow>=gNumRows)
- gCurrentRow=0;
- FramePosition(gCurrentRow, gCurrentColumn, TRUE);
- (**theData).offscreenNeedsUpdate=TRUE;
- break;
- }
-
- if (theValue!=-1)
- {
- if (Board[gCurrentRow][gCurrentColumn]!=theValue)
- {
- currentPoint.h=gCurrentColumn;
- currentPoint.v=gCurrentRow;
-
- if (theValue!=0)
- DoSound(sound_click, TRUE);
- else
- DoSound(sound_undo, TRUE);
-
- if (Board[gCurrentRow][gCurrentColumn]!=0)
- if (!DeleteOneMove(currentPoint))
- DebugStr("\pDeleteOneMove failed");
- if (theValue!=0)
- if (!AddOneMove(currentPoint))
- DebugStr("\pAddOneMove failed");
-
- Board[gCurrentRow][gCurrentColumn]=theValue;
- DrawOnePosition(gCurrentRow, gCurrentColumn, Board[gCurrentRow][gCurrentColumn], FALSE);
-
- (**theData).offscreenNeedsUpdate=TRUE;
- }
- }
-
- CheckEndGame(theData);
- }
-
- void MouseClickedInMainWindow(WindowDataHandle theData, Point thePoint)
- {
- short theRow, theColumn;
- short clickRow, clickColumn;
- Boolean gotone;
- Rect theRect;
-
- if (ShowingEndGameQQ())
- {
- DontShowEndGame(theData);
- return;
- }
-
- if (ShowingFindQQ())
- {
- DontShowFind(theData);
- return;
- }
-
- if (GameOverQQ())
- return;
-
- theRect.left=theRect.top=2;
- theRect.bottom=theRect.right=36;
- gotone=FALSE;
- for (theRow=0; ((theRow<gNumRows) && (!gotone)); theRow++)
- {
- for (theColumn=0; ((theColumn<gNumColumns) && (!gotone)); theColumn++)
- {
- if (PtInRect(thePoint, &theRect))
- {
- gotone=TRUE;
- clickRow=theColumn;
- clickColumn=theRow;
- }
-
- theRect.top+=38;
- theRect.bottom+=38;
- }
-
- theRect.left+=38;
- theRect.right+=38;
- theRect.top=2;
- theRect.bottom=38;
- }
-
- if (gotone)
- {
- if ((gCurrentRow!=clickRow) || (gCurrentColumn!=clickColumn))
- {
- FramePosition(gCurrentRow, gCurrentColumn, FALSE);
- FramePosition(clickRow, clickColumn, TRUE);
-
- gCurrentRow=clickRow;
- gCurrentColumn=clickColumn;
- }
- }
- }
-
- void DisposeTheMainWindow(WindowDataHandle theData)
- {
- AdjustMenus();
- DrawMenuBar();
- }
-
- void ActivateTheMainWindow(WindowDataHandle theData)
- {
- gOldForegroundTime=gForegroundWaitTime;
- gForegroundWaitTime=0;
- }
-
- void DeactivateTheMainWindow(WindowDataHandle theData)
- {
- gForegroundWaitTime=gOldForegroundTime;
- }
-
- void DrawTheMainWindow(WindowDataHandle theData, short theDepth)
- {
- RGBColor oldForeColor, oldBackColor;
- GrafPtr curPort;
- short i,j;
- Str31 theStr;
- Rect tempRect;
- short a, b, c, d;
-
- if (theDepth>2)
- {
- GetForeColor(&oldForeColor);
- GetBackColor(&oldBackColor);
- }
-
- GetPort(&curPort);
- EraseRect(&(curPort->portRect));
-
- if (ShowingEndGameQQ())
- {
- DrawEndGame(theData, theDepth);
- }
- else
- {
- for (i=0; i<gNumRows; i++)
- {
- for (j=0; j<gNumColumns; j++)
- {
- tempRect.top=i*38+2;
- tempRect.bottom=tempRect.top+36;
- tempRect.left=j*38+2;
- tempRect.right=tempRect.left+36;
- FrameRect(&tempRect);
-
- if ((gCurrentRow==i) && (gCurrentColumn==j) && (!ShowingFindQQ()) &&
- (!GameOverQQ()))
- {
- FramePosition(i, j, TRUE);
- }
-
- if (Board[i][j]>0)
- {
- InsetRect(&tempRect, 2, 2);
- if (gHasColorQD)
- PlotCIcon(&tempRect, gTheCIcon[Board[i][j]-1]);
- else
- PlotIcon(&tempRect, gTheIcon[Board[i][j]-1]);
- }
- }
- }
-
- if (ShowingFindQQ())
- {
- GetProblemPositions(&a, &b, &c, &d);
- DrawOnePosition(a, b, Board[a][b], TRUE);
- DrawOnePosition(c, d, Board[c][d], TRUE);
- }
- }
-
- if (theDepth>2)
- {
- RGBForeColor(&oldForeColor);
- RGBBackColor(&oldBackColor);
- }
- }
-